import QQMapWX from './qqmap-wx-jssdk.min';

const qqmapsdk = new QQMapWX({
  key:'Y5WBZ-W63CR-HRKW3-WEVWB-SXXFZ-NUBV4'
})
//获取用户定位信息
export function getLocation(){
  return new Promise((resolve,reject) => {
    wx.getSetting({
      success(res){
        const scopeUserLocation = res.authSetting['scope.userLocation']
        console.log(scopeUserLocation);
        // resolve(11)
        if(!scopeUserLocation){
          wx.authorize({
            scope: 'scope.userLocation',
            success(res){
              console.log(res);
              // resolve(res)
              wx.getLocation({
                altitude: 'true',
                success(res){
                  resolve(res)
                }
              })
            },
            fail(err){
              reject('使用该功能需要设置允许获取定位授权，请前往设置授权')
            }
          })
        }else{
          wx.getLocation({
            altitude: 'true',
            success(res){
              resolve(res)
            }
          })
        }
     }
    })
  })
}
//地点搜索
export function mapSearch(keyword,location){
  return new Promise((resolve,reject) => {
    qqmapsdk.search({
      keyword:keyword,
      location:location,
      page_size:10,
      auto_extend:'1',
      success(res){
        resolve(res)
      },
      fail(err){
        reject(err)
      }
    })
  })
}
//路线规划
export function mapdirection(from,to){
  return new Promise((resolve,reject) => {
    qqmapsdk.direction({
      mode:'walking',
      from,
      to,
      success(res){
        resolve(res)
      },
      fail(err){
        reject(err)
      }
    })
  })
}